﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using Microsoft.Telco.Controls.Properties;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Microsoft.Telco.Controls
{
    /// <summary>
    /// The phone number control
    /// </summary>

    #region Parts and States Model Declarations
    [TemplatePart(Name = "CountryPickerControl", Type = typeof(CountryPicker))]
    [TemplatePart(Name = "PhoneNumberTextbox", Type = typeof(TextBox))]
    [TemplatePart(Name = "ClearBorder", Type = typeof(Border))]
    #endregion

	public class PhoneNumberControl : TelcoControlBase
    { 
        #region ctor
        /// <summary>
        /// Initializes a new instance of the <see cref="PhoneNumberControl"/> class.
        /// </summary>
        public PhoneNumberControl()
        {
            this.DefaultStyleKey = typeof(PhoneNumberControl);
            this.PhoneNumberBoxDefaultText = ControlResources.PhoneNumberTextbox_DefaultText;
        } 
        #endregion

        #region parts
        /// <summary>
        /// Gets or sets the country picker.
        /// </summary>
        /// <value>The control to choose country from a dropdown.</value>
        public CountryPicker CountryPicker { get; set; }

        /// <summary>
        /// Gets or sets the phone number textbox.
        /// </summary>
        /// <value>The phone number textbox.</value>
        public TextBox PhoneNumberTextbox { get; set; }

        /// <summary>
        /// Gets or sets the clear border.
        /// </summary>
        /// <value>The clear border.</value>
        public Border ClearBorder { get; set; }
        #endregion

        #region properties
        /// <summary>
        /// Gets or sets the phone number.
        /// </summary>
        /// <value>The phone number.</value>
        public string PhoneNumber
        {
            get 
            {
                var trimmedText = PhoneNumberTextbox.Text.Trim();
                return trimmedText == PhoneNumberBoxDefaultText ? string.Empty : trimmedText;
            }
            set { PhoneNumberTextbox.Text = value; }
        }

        /// <summary>
        /// Gets or sets the phone number box default text.
        /// </summary>
        /// <value>The text to be displayed by default in the phone number textbox.</value>
        public string PhoneNumberBoxDefaultText { get; set; }

        /// <summary>
        /// Gets the selected country code.
        /// </summary>
        /// <value>The selected country code.</value>
        public string SelectedCountryCode
        {
            get
            {
                return this.CountryPicker.SelectedCountryCode;
            }
        }
        #endregion

        #region dependency properties
        /// <summary>
        /// Gets or sets the phone number text font family.
        /// </summary>
        /// <value>The phone number text font family.</value>
        public FontFamily PhoneNumberTextFontFamily
        {
            get { return (FontFamily)GetValue(PhoneNumberTextFontFamilyProperty); }
            set { SetValue(PhoneNumberTextFontFamilyProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextFontFamilyProperty =
            DependencyProperty.Register("PhoneNumberTextFontFamily", typeof(FontFamily), typeof(PhoneNumberControl),
            new PropertyMetadata(new FontFamily("Georgia")));

        /// <summary>
        /// Gets or sets the size of the phone number text font.
        /// </summary>
        /// <value>The size of the phone number text font.</value>
        public double PhoneNumberTextFontSize
        {
            get { return (double)GetValue(PhoneNumberTextFontSizeProperty); }
            set { SetValue(PhoneNumberTextFontSizeProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextFontSizeProperty =
            DependencyProperty.Register("PhoneNumberTextFontSize", typeof(double), typeof(PhoneNumberControl),
            new PropertyMetadata(21.0D));

        /// <summary>
        /// Gets or sets the phone number text foreground.
        /// </summary>
        /// <value>The phone number text foreground.</value>
        public Brush PhoneNumberTextForeground
        {
            get { return (Brush)GetValue(PhoneNumberTextForegroundProperty); }
            set { SetValue(PhoneNumberTextForegroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextForegroundProperty =
            DependencyProperty.Register("PhoneNumberTextForeground", typeof(Brush), typeof(PhoneNumberControl),
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF,0x33,0x66,0x99))));

        /// <summary>
        /// Gets or sets the phone number text background.
        /// </summary>
        /// <value>The phone number text background.</value>
        public Brush PhoneNumberTextBackground
        {
            get { return (Brush)GetValue(PhoneNumberTextBackgroundProperty); }
            set { SetValue(PhoneNumberTextBackgroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for PhoneNumberTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PhoneNumberTextBackgroundProperty =
            DependencyProperty.Register("PhoneNumberTextBackground", typeof(Brush), typeof(PhoneNumberControl),
             new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF))));

        /// <summary>
        /// Gets or sets the country text font family.
        /// </summary>
        /// <value>The country text font family.</value>
        public FontFamily CountryTextFontFamily
        {
            get { return (FontFamily)GetValue(CountryTextFontFamilyProperty); }
            set { SetValue(CountryTextFontFamilyProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryTextFontFamily.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryTextFontFamilyProperty =
            DependencyProperty.Register("CountryTextFontFamily", typeof(FontFamily), typeof(PhoneNumberControl),
            new PropertyMetadata(new FontFamily("Arial")));

        /// <summary>
        /// Gets or sets the size of the country text font.
        /// </summary>
        /// <value>The size of the country text font.</value>
        public double CountryTextFontSize
        {
            get { return (double)GetValue(CountryTextFontSizeProperty); }
            set { SetValue(CountryTextFontSizeProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryTextFontSize.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryTextFontSizeProperty =
            DependencyProperty.Register("CountryTextFontSize", typeof(double), typeof(PhoneNumberControl),
            new PropertyMetadata(12.0D));

        /// <summary>
        /// Gets or sets the country text foreground.
        /// </summary>
        /// <value>The country text foreground.</value>
        public Brush CountryTextForeground
        {
            get { return (Brush)GetValue(CountryTextForegroundProperty); }
            set { SetValue(CountryTextForegroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryTextForeground.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryTextForegroundProperty =
            DependencyProperty.Register("CountryTextForeground", typeof(Brush), typeof(PhoneNumberControl),
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0x00))));

        /// <summary>
        /// Gets or sets the country dropdown background.
        /// </summary>
        /// <value>The country dropdown background.</value>
        public Brush CountryDropdownBackground
        {
            get { return (Brush)GetValue(CountryDropdownBackgroundProperty); }
            set { SetValue(CountryDropdownBackgroundProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for CountryDropdownBackground.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CountryDropdownBackgroundProperty =
            DependencyProperty.Register("CountryDropdownBackground", typeof(Brush), typeof(PhoneNumberControl),
            new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF))));
        #endregion

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) call <see cref="M:System.Windows.Controls.Control.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            CountryPicker = GetTemplateChild("CountryPickerControl") as CountryPicker;
            PhoneNumberTextbox = GetTemplateChild("PhoneNumberTextbox") as TextBox;
            ClearBorder = GetTemplateChild("ClearBorder") as Border;

            if (CountryPicker != null)
            {
                CountryPicker.SelectedCountryChanged += CountryPicker_SelectedCountryChanged;
            }

            if (PhoneNumberTextbox != null)
            {
                PhoneNumberTextbox.GotFocus += PhoneNumberTextbox_GotFocus;
                PhoneNumberTextbox.LostFocus += PhoneNumberTextbox_LostFocus;
                PhoneNumberTextbox.Text = PhoneNumberBoxDefaultText;
            }

            if (ClearBorder != null)
            {
                ClearBorder.MouseLeftButtonDown += new MouseButtonEventHandler(ClearBorder_MouseLeftButtonDown);
            }
        }       

        #region event handlers
        private void ClearBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PhoneNumberTextbox.Text = string.Empty;
            PhoneNumberTextbox.Focus();
            CountryPicker.SelectFlagFromCountryCode(string.Empty);
        }

        private void CountryPicker_SelectedCountryChanged(string countryCode)
        {
            PhoneNumberTextbox.Text = string.Format("{0}", countryCode);
            PhoneNumberTextbox.Opacity = 1;
        }

        private void PhoneNumberTextbox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (PhoneNumberTextbox.Text.Trim() == PhoneNumberBoxDefaultText)
            {
                PhoneNumberTextbox.Text = this.CountryPicker.SelectedCountryCode;
                PhoneNumberTextbox.SelectionStart = this.CountryPicker.SelectedCountryCode.Length;
            }
            PhoneNumberTextbox.Opacity = 1;
            ControlHelper.HideAllPopups();
        }

        private void PhoneNumberTextbox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(PhoneNumberTextbox.Text))
            {
                PhoneNumberTextbox.Opacity = 0.5;
                PhoneNumberTextbox.Text = PhoneNumberBoxDefaultText;
            }
        } 
        #endregion
	}
}
